<?php
ob_start();
session_start();
error_reporting(0);
ini_set("memory_limit","100M");
require_once("../includes/dbconfig.php");
require_once("includes/functions.php");
require_once("includes/settings.php");
require_once("includes/geoiploc.php");




$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);


$ip = getIP();
$country = geoip_country_code_by_addr($gi, getIP());

/*

This file will accept offer id, and use admin api to fetch offer destination url,  and redirect user to advertiser.

*/



if(isset($_GET['id']) && isset($_GET['s']) && $_GET['gid'])
{
  $offer_id = makesafe(safeGet($_GET['id']));	
  $sessId = makesafe(safeGet($_GET['s']));
  $gid = makesafe(safeGet($_GET['gid']));
  $hash = $sessId;
  
  
  if(empty($offer_id) || empty($sessId) || empty($gid))
  die("Something went wrong, please close this page and refresh the content locker page.");
  
  
  
  //Register Session with widget session and offers for this widget clicked.
  $_SESSION[SITE_NAME.'HSTGW_cl_sessionId_xkldID'.$sessId] = $gid;
  

  if(isset($_SESSION[SITE_NAME.'HSTXU_XEX_GWID_SESS_Offers'.$sessId]))
  {
	  $session_offers = $_SESSION[SITE_NAME.'HSTXU_XEX_GWID_SESS_Offers'.$sessId];	
	  
	  if(isset($session_offers[$offer_id]) && $session_offers[$offer_id] == 'complete')
	  {
		    die("This offer has been already clicked.");  
	  }
	  
	  $session_offers[$offer_id] = 'clicked'; // the offer is just clicked.
	  
	  
	  $_SESSION[SITE_NAME.'HSTXU_XEX_GWID_SESS_Offers'.$sessId] = $session_offers;
  }else
  {
	  $session_offers[$offer_id] = 'clicked'; // the offer is just clicked.			
	  $_SESSION[SITE_NAME.'HSTXU_XEX_GWID_SESS_Offers'.$sessId] = $session_offers;
  }
  
  if(mysql_num_rows(mysql_query("SELECT id FROM gw_session_offers WHERE session_id = '$sessId AND gid = '$gid' AND offer_id = '$offer_id' AND status = 'complete'")))
  die("you have already completed this offer.");
  
 
 
  //@mysql_query("DELETE FROM gw_session_offers WHERE session_id = '$sessId' AND gid = '$gid' AND offer_id = '$offer_id' AND status = 'pending'");
  
  
  //get user details and offer details
  $gsql = mysql_query("SELECT gid, uid, unlock_period FROM gateways WHERE gid = '$gid'");
  if(!mysql_num_rows($gsql))
  die("Invalid Gateway");
  $gro = mysql_fetch_object($gsql);
  $aff_id = $gro->uid;
  $uid = $aff_id;  
  
  
  $sql2 = mysql_query("SELECT * FROM gw_offers WHERE campaign_id = '$offer_id' AND active = 1 LIMIT 1");
  if(!mysql_num_rows($sql2))
  {
	  die("Invalid Offer");
  }
  
  $fro = mysql_fetch_object($sql2);
  $credits = $fro->credits;
  $offerName = $fro->name;
  
  
  
	//Get user settings
	
	$creditMode = 'Default';

//    $ref_credits = sprintf($credits * (REFERRAL_RATE / 100));	
	$points = sprintf($credits * (OFFER_RATE / 100));	
	
 
    //get the offer
  
	$sql1 = mysql_query("SELECT *  FROM gw_offers  WHERE campaign_id = '$offer_id' AND (countries LIKE '%".$country."%' OR countries = 'All') AND (`hits` < `limit` OR `limit` = 0) AND active = 1 LIMIT 1");
	if(!mysql_num_rows($sql1))
	{
        die("Invalid Offer!");
		return;
	}
	
	
	$row = mysql_fetch_object($sql1);
	$link = $row->link;
	$network = $row->network;
	$campaignId = stripslashes($row->campaign_id);  
  
  //end getting offer

	
  @mysql_query("DELETE FROM gw_session_offers WHERE uid = '$uid' AND offer_id = '$offer_id' AND session_id = '$sessId' AND network = '$network'");
  if(!(mysql_query("INSERT INTO gw_session_offers VALUES (NULL, '$sessId', '$gid', '$offer_id', 'pending', NOW(), '$offerName', '$uid', '$points', '0', '$network', '$creditMode', '$country', '0', '1', '$ip')")))
  die("Some error occured, please try other offer.");	
		  

    //update hits
	@mysql_query("UPDATE gw_offers SET hits = hits+1 WHERE campaign_id = '$offer_id' AND network = '$network' LIMIT 1");
	 setGWEpc($offer_id, $network);
		
		
	
			
				
			
				
		if($network == 'Adgatemedia')
        $offer_url = $link."&aff_sub=$gid&aff_sub2=$sessId&sub=$gid&sub2=$sessId&aff_sub4=gateway";		
		elseif($network == 'Adworkmedia')
		$offer_url = $link."&sid=$gid&sid2=$sessId&sid3=gateway";
		elseif($network == 'Maxbounty')
		$offer_url = $link."&s1=$gid-itsGW&s2=$sessId";
		elseif($network == 'Adscendmedia' || $network == 'CPALead')
		$offer_url = $link."$gid-$sessId-itsGW";	
     	elseif($network == 'adshiftmedia')
		$offer_url = $link."$gid&sid2=$sessId&sid3=gateway";	
     	elseif($network == 'adnooka' || $network == "Adnooka")
		$offer_url = $link."&sid=$gid&sid2=$sessId&sid3=gateway";							
		else
		$offer_url = $link."$gid-$sessId-itsGW";

	   

	     header("location: $offer_url");	
		 exit;
		
		
		
	
	}	
	
	
	

	


?>